<div class="layuimini-container layui-anim layui-anim-upbit">
    <div class="layuimini-main">

        <div style="margin-top:5px">
            当前服务器版本：<span id="main_serverVersion"></span>
            <button id="main_check_update" class="layui-btn layui-btn-xs"> 检查并更新 </button>
        </div>

        <table class="layui-hide" id="modTable" lay-filter="modTableFilter"></table>
    </div>
</div>

<script>
    layui.use(['form', 'table','layuimini'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layuimini = layui.layuimini;
        var loadIndex = layer.load();
        $.ajax({
            url: '/api/get_server_info',
            type: 'POST',
            dataType: 'JSON',
            success: function (data) {
                layer.close(loadIndex);
                if (data.code) {
                    layer.open({
                        title: '提示',
                        content: data.msg
                    });
                    return;
                }
                $('#main_serverVersion').html(data.data.serverVersion);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                layer.close(loadIndex);
                layer.open({
                    title: '提示',
                    content: '糟糕，出问题了。'
                });
            }
        });

        $('#main_check_update').on('click', function () {
            var loadIndex = layer.load();
            $.ajax({
                url: '/api/check_and_update',
                type: 'POST',
                dataType: 'JSON',
                success: function (data) {
                    layer.close(loadIndex);
                    if (data.code == 1) {
                        layer.open({
                            title: '提示',
                            content: data.msg
                        });
                        return;
                    }

                    layer.open({
                        title: '更新日志',
                        type: 1,
                        content: `<div style="padding: 20px 20px 0px 20px;"><div id="readClusterLog" class="layui-row">
                        <div class="layui-col-xs6">
                                <pre id="check_and_update_log" style="white-space: pre"></pre>
                        </div></div></div>`,
                        area: ['600px', '400px'],
                        
                        end: function (index, layero) {
                            clearInterval(timer);
                        }
                    });

                    if (data.code == 2) {
                        layer.msg('当前更新指令未执行完成，日志提供本次输出内容。');
                    }

                    var cnt = 0;
                    var requesting = false;
                    timerFunc();
                    var timer = setInterval(timerFunc, 2000);
                    function timerFunc() {
                        if (++cnt > 120) { // 4分钟
                            clearInterval(timer);
                            layer.open({
                                title: '提示',
                                content: '日志查看超时'
                            });
                            return;
                        }
                        if (!requesting) {
                            requesting = true;
                            $.ajax({
                                url: '/api/check_and_update_result',
                                type: 'POST',
                                dataType: 'JSON',
                                success: function (data) {
                                    requesting = false;
                                    if (data.code) {
                                        clearInterval(timer);
                                        layer.open({
                                            title: '提示',
                                            content: data.msg
                                        });
                                        return;
                                    }
                                    if (!data.updating) {
                                        clearInterval(timer);
                                        layer.open({
                                            title: '提示',
                                            content: '指令执行结束，请查看日志.'
                                        });
                                    }
                                    $('#check_and_update_log').html(data.data);
                                },
                                error: function () {
                                    requesting = false;
                                    clearInterval(timer);
                                }
                            })
                        }
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    layer.close(loadIndex);
                    layer.open({
                        title: '提示',
                        content: '糟糕，出问题了。'
                    });
                }
            });
        });
    });
</script>